// pages/EatWhat/EatWhat.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carArray: [
      // { "name": "张三", "number": "闽D1253", "type": "玛莎拉蒂", "startTime": "2018-05-09 18:30", "endTime": "2018-05-09 18:30"},
      // { "name": "李四", "number": "闽D1254", "type": "Lara", "startTime": "2018-05-09 18:30", "endTime": "2018-05-09 18:30"}
      // ,
      // { "name": "王五", "number": "闽D1255", "type": "阿斯顿马丁", "startTime": "2018-05-09 18:30","endTime": "2018-05-09 18:30" },
      // { "name": "王五", "number": "闽D1255", "type": "阿斯顿马丁", "startTime": "2018-05-09 18:30", "endTime": "2018-05-09 18:30" },
      // { "name": "王五", "number": "闽D1255", "type": "阿斯顿马丁", "startTime": "2018-05-09 18:30", "endTime": "2018-05-09 18:30" }
    ],

    //Order Status
    ordering:1,
    finished:0,

    boxVisibilityArray:[],
    labelVisibilityArray:[],
    styleVisibilityArray:[],
    currentIndex : 0 ,

    
    serverUrl: {
      getAll: getApp().globalData.serverUrlPrefix+'/CarRental/Order/ShowAll',
      cancel: getApp().globalData.serverUrlPrefix+'/CarRental/Order/CancelOrder'
    },


    currentTab: 0,
    tabStyle: ["tabBarLabel-selected", "tabBarLabel","tabBarLabel"],
  },

  orderButtonClicked:function(e){
    var id = e.currentTarget.id;
    var value = this.data.carArray[id];
    if (value.status == this.data.finished){
        return false;
    }

    console.log('i am here');
    this.requestCancelOrder(value);
  },


  updateBoxVisibility:function(tab){

    this.data.currentIndex = tab;

    var valueTrue = true;
    var valueFalse = false;

    if(tab == 0){
      valueFalse = true;
    } else if (tab == 2){
      valueTrue = false;
      valueFalse = true;
    }
    // remain the same if it's tab 1 : 预约中

    var array = [];   
    var textArray = [];
    var styleArray = [];

    var data = this.data.carArray;

    var len = data.length;
    for (var i = 0; i < len; i++) {
      var status = data[i].status;
      //Update the Visibility of the lineBox
      if(status == this.data.ordering){
        array.push(valueTrue);
      }else{
        array.push(valueFalse);
      }
      //Update the button text
      if(status == 1){
        textArray.push("取消");
        styleArray.push("rightButton");
      }else{
        textArray.push("已结束");
        styleArray.push("rightButton-closed");
      }
    }

    this.setData(
      {
        boxVisibilityArray:array ,
        labelVisibilityArray: textArray,
        styleVisibilityArray: styleArray
      }
    )

    // console.log(array);
  },


///Network
  requestAllOrders:function(){
    console.log("Requesting all orders");
    var self = this;
    var curUserID = getApp().globalData.userID;
    wx: wx.request({
      url: this.data.serverUrl.getAll, //Show all Orders
      data: {
        userID: curUserID   //Debug
      },
      header: {
        'content-type': 'application/json;' // 默认值
      },
      success: function (res) {
        // console.log(self);
        console.log("Orders:Receive data from server");
        // console.log(res.data);
        self.parseServerData(res.data);
      },
      fail: function (res) {
        self.showMessage('网络故障');
      },
      complete: function (res) { }
    })
  },
  requestCancelOrder: function (value) {
    console.log("Requesting to cancel");
    var self = this;
    var curUserID = getApp().globalData.userID;
    wx: wx.request({
      url: this.data.serverUrl.cancel, //Show all Orders
      data: {
        orderID: value.id   //curUserID   //Debug
      },
      header: {
        'content-type': 'application/json;' // 默认值
      },
      success: function (res) {
        // console.log(self);
        console.log(res.data);
        // console.log(res.data);
        self.requestAllOrders();
      },
      fail: function (res) {
        self.showMessage('网络故障');
      },
      complete: function (res) { }
    })
  },
  

  //Parsing carList data from Server
  parseServerData: function (data) {
    console.log(data)
    console.log("Data Size:" + data.length);
    this.setData(
      {
        carArray: data
      }
    )

    this.updateBoxVisibility(this.data.currentIndex);
  },

  showMessage: function (message) {
    console.log('Message:' + message);
    wx: wx.showToast({
      title: message,
      icon: 'none',
      duration: 3000,
    })
  },

///TabBar
  tabButtonClicked:function(e){
    // console.log("Clicked:" + e.currentTarget.id);
    var index = e.currentTarget.id;
    this.updateTab(index);

    this.updateBoxVisibility(index);
  },

  updateTab:function(index){
    // console.log("Cur Tab:"+index);
    var tabs = ["tabBarLabel", "tabBarLabel", "tabBarLabel"];
    tabs[index] = "tabBarLabel-selected";
      this.setData(
      {
        tabStyle: tabs
      }
    );
  },


///Scroll View Handler
  upper: function (e) {
    // console.log(e)
  },
  lower: function (e) {
    // console.log(e)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestAllOrders();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.updateBoxVisibility(0);
   
    var globalUserName = getApp().globalData.userName ;
    this.setData(
      {
        userName : globalUserName
      }
    )
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})